package com.sit.config;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        if (uri.contains("/reader/loginHtml") || uri.contains("/reader/registerHtml") || uri.contains("/reader/forgetPassHtml") || uri.contains("/reader/login") || uri.contains("/reader/addReader") || uri.contains("/reader/forgetPass") || uri.contains("/reader/sendEmail")) {
            return true;
        }
        if(uri.contains("/captcha") || uri.contains("/verify")){
            return true;
        }
        if(uri.contains("/admin/adminloginHtml") || uri.contains("/admin/login")){
            return true;
        }
        HttpSession session = request.getSession();
        if (session.getAttribute("readId") != null || session.getAttribute("adminId") != null) {
            return true;
        }
        response.sendRedirect(request.getContextPath() + "/reader/loginHtml");
        return false;
    }
}
